package com.raidpoints.client;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextBox;

public class PointsEditor extends DialogBox {

	private ScrollPanel bidPan;
	FlexTable bidTable;
	ArrayList<String> bidList;
	private ListBox poolBox;
	RaidPoints rp;
	private CheckBox npBox;
	private TextBox pphBox;
	private TextBox standbyBox;
	private CheckBox standbypphBox;

	public PointsEditor(RaidPoints mrp) {
		rp = mrp;
		//neg10,pph,standbypoints,standbypph10,bidname:points,....
		//0,4,0,0,Shroud:50%,Shroud->Standard:10,Standard:10,Save:10
		setHTML("Ruleset Editor");
		
		LayoutPanel negBox = new LayoutPanel();
		setWidget(negBox);
		negBox.setSize("381px", "335px");
		
		bidPan = new ScrollPanel();
		negBox.add(bidPan);
		negBox.setWidgetLeftWidth(bidPan, 10.0, Unit.PX, 361.0, Unit.PX);
		negBox.setWidgetTopHeight(bidPan, 40.0, Unit.PX, 172.0, Unit.PX);
		
		Label lblPointsPerHour = new Label("Points per hour:");
		negBox.add(lblPointsPerHour);
		negBox.setWidgetLeftWidth(lblPointsPerHour, 0.0, Unit.PX, 102.0, Unit.PX);
		negBox.setWidgetTopHeight(lblPointsPerHour, 252.0, Unit.PX, 16.0, Unit.PX);
		
		Label lblNewLabel = new Label("Standby points:");
		negBox.add(lblNewLabel);
		negBox.setWidgetLeftWidth(lblNewLabel, 150.0, Unit.PX, 102.0, Unit.PX);
		negBox.setWidgetTopHeight(lblNewLabel, 252.0, Unit.PX, 16.0, Unit.PX);
		
		npBox = new CheckBox("Negative Points");
		negBox.add(npBox);
		negBox.setWidgetLeftWidth(npBox, 212.0, Unit.PX, 140.0, Unit.PX);
		negBox.setWidgetTopHeight(npBox, 227.0, Unit.PX, 19.0, Unit.PX);
		
		poolBox = new ListBox();
		poolBox.setWidth("210px");
		negBox.add(poolBox);
		negBox.setWidgetLeftWidth(poolBox, 80.0, Unit.PX, 219.0, Unit.PX);
		negBox.setWidgetTopHeight(poolBox, 10.0, Unit.PX, 24.0, Unit.PX);
		
		Button saveButton = new Button("Save");
		negBox.add(saveButton);
		negBox.setWidgetLeftWidth(saveButton, 80.0, Unit.PX, 81.0, Unit.PX);
		negBox.setWidgetTopHeight(saveButton, 293.0, Unit.PX, 28.0, Unit.PX);
		
		Button cancelButton = new Button("Cancel");
		negBox.add(cancelButton);
		negBox.setWidgetLeftWidth(cancelButton, 212.0, Unit.PX, 81.0, Unit.PX);
		negBox.setWidgetTopHeight(cancelButton, 293.0, Unit.PX, 28.0, Unit.PX);
		
		pphBox = new TextBox();
		pphBox.setMaxLength(3);
		pphBox.setWidth("38px");
		negBox.add(pphBox);
		negBox.setWidgetLeftWidth(pphBox, 94.0, Unit.PX, 52.0, Unit.PX);
		negBox.setWidgetTopHeight(pphBox, 255.0, Unit.PX, 32.0, Unit.PX);
		
		standbyBox = new TextBox();
		standbyBox.setMaxLength(3);
		standbyBox.setWidth("38px");
		negBox.add(standbyBox);
		negBox.setWidgetLeftWidth(standbyBox, 241.0, Unit.PX, 52.0, Unit.PX);
		negBox.setWidgetTopHeight(standbyBox, 252.0, Unit.PX, 32.0, Unit.PX);
		
		standbypphBox = new CheckBox("PPH");
		negBox.add(standbypphBox);
		negBox.setWidgetLeftWidth(standbypphBox, 296.0, Unit.PX, 75.0, Unit.PX);
		negBox.setWidgetTopHeight(standbypphBox, 249.0, Unit.PX, 19.0, Unit.PX);
		
		final Button bidButton = new Button();
		bidButton.setText("New Bid");
		negBox.add(bidButton);
		negBox.setWidgetLeftWidth(bidButton, 10.0, Unit.PX, 75.0, Unit.PX);
		negBox.setWidgetTopHeight(bidButton, 218.0, Unit.PX, 28.0, Unit.PX);
		
		bidButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				bidList.add("New Bid " + bidList.size() + ":0");	
				DrawBidTable();
			}
		});
		bidButton.setEnabled(false);
		
		cancelButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				hide();				
			}
		});
		
		saveButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(poolBox.getSelectedIndex() <= 0) return;
				if(!pphBox.getText().matches("[0-9]{1,3}") || !standbyBox.getText().matches("[0-9]{1,3}")){
					Window.alert("Invalid points per hour entry");
					return;
				}
				//neg10,pph,standbypoints,standbypph10,bidname:points,....
				StringBuilder mrset = new StringBuilder();
				mrset.append(npBox.getValue() ? "1" : "0");
				mrset.append("," + pphBox.getText());
				mrset.append("," + standbyBox.getText());
				mrset.append("," + (standbypphBox.getValue() ? "1" : "0"));
				for(String s : bidList) mrset.append("," + s);
				rp.rpsvc.SavePoolRuleset(poolBox.getItemText(poolBox.getSelectedIndex()), mrset.toString(), rp.alertCallback);
				hide();
			}
		});
		
		bidList = new ArrayList<String>();
		bidTable = new FlexTable();
		bidPan.add(bidTable);
		
		AsyncCallback<ArrayList<String>> poolsCallback = new AsyncCallback<ArrayList<String>>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ArrayList<String> result) {
				if(result == null) return;
				poolBox.clear();
				poolBox.addItem("Select Points Pool:");
				for(String s : result) poolBox.addItem(s);
			}
			
		};
						
		poolBox.addChangeHandler(new ChangeHandler(){
			@Override
			public void onChange(ChangeEvent event) {
				if(poolBox.getSelectedIndex() > 0){
					AsyncCallback<String> ruleCallback = new AsyncCallback<String>(){

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(String result) {
							if(result != null){
								bidButton.setEnabled(true);
								String[] rset = result.split(",");
								npBox.setValue(rset[0].equals("1"));
								pphBox.setValue(rset[1]);
								standbyBox.setValue(rset[2]);
								standbypphBox.setValue(rset[3].equals("1"));
								bidList.clear();
								for(int i = 4; i < rset.length; i++) bidList.add(rset[i]);
								DrawBidTable();
							}
						}
						
					};
					rp.rpsvc.GetPoolRuleset(poolBox.getItemText(poolBox.getSelectedIndex()),ruleCallback);
				}
				else{
					bidList.clear();
					bidButton.setEnabled(false);
					DrawBidTable();
				}
			}
		});
		
		rp.rpsvc.GetEditablePools(poolsCallback);
	}
	
	void DrawBidTable(){
		bidTable.clear();
		bidTable.setWidget(0,0,new Label("Bid Name"));
		bidTable.setWidget(0,1,new Label("Points (cost or %)"));
		for(int i = 0; i < bidList.size(); i++){
			String bt = bidList.get(i);
			final TextBox nameBox = new TextBox();
			final TextBox pointBox = new TextBox();
			final TextBox minBox = new TextBox();
			Button removeButton = new Button("-");
			nameBox.setWidth("160px"); pointBox.setWidth("50px"); minBox.setWidth("30px");
			//minBox.setVisible(false); 
			minBox.setText("0");
			
			bidTable.setWidget(i+1, 0, nameBox);
			bidTable.setWidget(i+1, 1,pointBox);
			bidTable.setWidget(i+1, 2, minBox);
			bidTable.setWidget(i+1, 3, removeButton);
			bidTable.getCellFormatter().setVisible(i+1, 2, false);
			final int cr = i;
			
			nameBox.setText(bt.split(":")[0]);
			String ns = bt.split(":")[1];
			if(ns.contains("%")){
				pointBox.setText(ns.substring(0,ns.indexOf("%")+1));
				minBox.setText(ns.substring(ns.indexOf("%")+1,ns.length()));
				if(minBox.getText().equals("")) minBox.setText("0");
				bidTable.getCellFormatter().setVisible(i+1, 2, true);
				//minBox.setVisible(true);
			}
			else{
				pointBox.setText(ns);
			}
			
			

			removeButton.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					bidList.remove(cr);
					DrawBidTable();
				}
			});
			
			nameBox.addChangeHandler(new ChangeHandler(){
				@Override
				public void onChange(ChangeEvent event) {
					String nn = nameBox.getText();
					if(!nn.matches("[a-zA-Z0-9-<>/ ]{1,50}")){
						nameBox.setText("New Bid " + cr);
						nn = "New Bid " + cr;
					}
					for(int z = 0; z < bidList.size(); z++){
						if(z != cr && bidList.get(z).split(":")[0].equals(nn)){
							nameBox.setText("New Bid " + cr);
							nn = "New Bid " + cr;
							break;
						}
					}
					nn = nn + ":" + pointBox.getText() + (pointBox.getText().contains("%") ? minBox.getText() : "");
					bidList.set(cr, nn);
				}
			});
			
			pointBox.addChangeHandler(new ChangeHandler(){
				@Override
				public void onChange(ChangeEvent event) {
					String nn = pointBox.getText();
					if(!nn.matches("[0-9]{1,3}[%]{0,1}")){
						pointBox.setText("0");
						nn = "0";
					}
					if(nn.contains("%")) bidTable.getCellFormatter().setVisible(cr+1, 2, true);
					else bidTable.getCellFormatter().setVisible(cr+1, 2, false);
					nn = nameBox.getText() + ":" + nn + (nn.contains("%") ? minBox.getText() : "");
					bidList.set(cr, nn);
				}
			});
			
			minBox.addChangeHandler(new ChangeHandler(){
				@Override
				public void onChange(ChangeEvent event) {
					String nn = minBox.getText();
					if(!nn.matches("[0-9]{1,3}")){
						minBox.setText("0");
						nn = "0";
					}
					nn = nameBox.getText() + ":" + pointBox.getText() + nn;
					bidList.set(cr, nn);
				}
			});
			
		}
		
	}
}
